﻿using DocumentGeneratorPlugins;
using OpenXmlDocumentGenerator;
using OpenXMLDocumentGenerator.Helpers;
using OpenXMLDocumentGenerator.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WMIDocumentor
{
    public class WMIAutoDocumentor:ISystemDocumentor
    {
        public DocumentorLog.Logger Log
        {
            get
            {
                return DocumentorLog.Logger.Current;
            }
        }

        public event DocumentGeneratorEventHandler DocumentGeneratorEvent;

        public void GenerateDocument(IDocumentOptions options)
        {
            OnDocumentGeneratorEvent("Get WMI information", StateStep.Start);
            WMIModel.WMIComputer comp=WMIHelper.WMILoader.GetComputerInformation();
            OnDocumentGeneratorEvent("Start Creating Document", StateStep.Progress);
            WordDocumentHelpers wdh = new WordDocumentHelpers();
            wdh.CreatePackage(options);
            FillBiosInformation(comp.BiosInformation, wdh);
            FillCDRomInformation(comp.CDRomInformation, wdh);
            FillComputerSystemInformation(comp.ComputerSystemInformation, wdh);
            FillComputerSystemProductInformation(comp.ComputerSystemProductInformation, wdh);
            FillDiskInformation(comp.DiskInformation, wdh);
            FillIP4RouteTable(comp.IP4RouteTable, wdh);
            FillNTEventLog(comp.NTEventLog, wdh);
            FillOSInformation(comp.OSInformation, wdh);
            FillPhysicalMemory(comp.PhysicalMemory, wdh);
            FillProcess(comp.Process, wdh);
            FillProcessor(comp.Processor, wdh);
            FillProduct(comp.Product, wdh);
            FillQuickFixEngineering(comp.QuickFixEngineering, wdh);
            FillRegistry(comp.Registry, wdh);
            FillService(comp.Service, wdh);
            FillShare(comp.Share, wdh);
            FillSoundDevice(comp.SoundDevice, wdh);
            FillStartupCommand(comp.StartupCommand, wdh);
            FillTimerZone(comp.TimerZone, wdh);
            FillVideoController(comp.VideoController, wdh);
            wdh.Dispose();
            OnDocumentGeneratorEvent("Finish Creating Document", StateStep.Finish);
        }

        private void FillVideoController(WMIModel.WMIVideoController[] wMIVideoController, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Video Controller");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIVideoController element in wMIVideoController)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element,new string[]{});
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillTimerZone(WMIModel.WMITimeZone[] wMITimeZone, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Time Zone");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMITimeZone element in wMITimeZone)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillStartupCommand(WMIModel.WMIStartupCommand[] WMIStartupCommand, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Startup Command");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIStartupCommand element in WMIStartupCommand)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillSoundDevice(WMIModel.WMISoundDevice[] wMISoundDevice, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Sound Device");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMISoundDevice element in wMISoundDevice)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillShare(WMIModel.WMIShare[] wMIShare, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Share");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIShare element in wMIShare)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillService(WMIModel.WMIService[] wMIService, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Service");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIService element in wMIService)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillRegistry(WMIModel.WMIRegistry[] wMIRegistry, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Registry");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIRegistry element in wMIRegistry)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillQuickFixEngineering(WMIModel.WMIQuickFix[] wMIQuickFix, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("QuickFix");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIQuickFix element in wMIQuickFix)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillProduct(WMIModel.WMIProduct[] wMIProduct, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Product");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIProduct element in wMIProduct)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillProcessor(WMIModel.WMIProcessor[] wMIProcessor, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Processor");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIProcessor element in wMIProcessor)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillProcess(WMIModel.WMIProcess[] wMIProcess, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Processor");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIProcess element in wMIProcess)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillPhysicalMemory(WMIModel.WMIPhysicalMemory[] wMIPhysicalMemory, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Physical Memory");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIPhysicalMemory element in wMIPhysicalMemory)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillOSInformation(WMIModel.WMIOs[] wMIOs, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("OS Information");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIOs element in wMIOs)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillNTEventLog(WMIModel.WMIEventLog[] wMIEventLog, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("EventLog");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIEventLog element in wMIEventLog)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillIP4RouteTable(WMIModel.WMIIPV4[] wMIIPV4, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("IP4 Route Table");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIIPV4 element in wMIIPV4)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillDiskInformation(WMIModel.WMIDisk[] wMIDisk, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Disk Information");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIDisk element in wMIDisk)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillComputerSystemProductInformation(WMIModel.WMIComputerSystemProductInformation[] wMIComputerSystemProductInformation, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Computer System Product Information");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIComputerSystemProductInformation element in wMIComputerSystemProductInformation)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillComputerSystemInformation(WMIModel.WMIComputerSystemInformation[] wMIComputerSystemInformation, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Computer System Information");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIComputerSystemInformation element in wMIComputerSystemInformation)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillCDRomInformation(WMIModel.WMICD[] wMICD, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("CD");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMICD element in wMICD)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        private void FillBiosInformation(WMIModel.WMIBios[] wMIBios, WordDocumentHelpers wdh)
        {
            wdh.AddHeader1Paragraph();
            wdh.AddText("Bios");
            wdh.AddNormalParagraph();
            foreach (WMIModel.WMIBios element in wMIBios)
            {
                PropertyModel[] pms = RenderTypeHelper.GetPropertyFromObject(element, new string[] { });
                RenderTypeHelper.RenderPropertyModelForTable(pms, wdh);
                wdh.AddNormalParagraph();
            }
        }

        public IDocumentOptions CreateDocumentOption()
        {
            return new WMIDocumentorOptions();
        }

        public string PluginName
        {
            get { return "WMI Documentor"; }
        }

        public System.Windows.Controls.UserControl GetUserInterface()
        {
            return (System.Windows.Controls.UserControl)(new WMIDocumentor.UI.WMIDocumentorUI(this));
        }
        protected virtual void OnDocumentGeneratorEvent(String Message, StateStep Step)
        {
            if (DocumentGeneratorEvent != null)
            {
                DocumentGeneratorEventArgs dgea = new DocumentGeneratorEventArgs();
                dgea.Message = Message;
                dgea.State = Step;
                DocumentGeneratorEvent(this, dgea);
            }
        }
    }
}
